﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[CalculeazaToateStocurileLunare]
AS
BEGIN
	SET NOCOUNT ON;
	
	DECLARE @weeks int = 4;
	DECLARE @id_Medicament int;
	DECLARE @stocLunar int;
	DECLARE @month int = month(dateadd(m,1,getdate()));
	DECLARE @result TABLE
	(
		Id_Medicament int not null,
		StocLunar float 
	)
	
    DECLARE medicamentCursor CURSOR FOR
    SELECT Id_Medicament FROM Medicament
    
    OPEN medicamentCursor
    
    FETCH NEXT FROM medicamentCursor INTO @id_Medicament
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
		EXEC CalculeazaStocLunar @id_Medicament,@month, @weeks, @stocLunar output
		
		INSERT INTO @result (Id_Medicament,StocLunar)
		VALUES (@id_Medicament, @stocLunar)
		
		FETCH NEXT FROM medicamentCursor INTO @id_Medicament
    END
    
    CLOSE medicamentCursor
    DEALLOCATE medicamentCursor
    
    SELECT Id_Medicament, StocLunar
    FROM @result
END
